package edu.spec.wechat.user.group;

import edu.spec.wechat.kit.WechatKit;
import edu.spec.wechat.model.WechatFianlValue;
import edu.spec.wechat.quartz.RefreshAccessTokenTask;
import edu.spec.wechat.user.model.WechatGroup;
import edu.spec.wechat.web.WechatContext;
import org.madgecko.basic.util.JsonUtil;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created by MadFrog on 2016/10/26.
 */
@Service
public class WechatGroupService implements IWechatGroupService {
    @Override
    public void add(WechatGroup group) {
        String url = WechatFianlValue.ADD_GROUP;
        url = url.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
        Map<String, WechatGroup> map = new HashMap<String, WechatGroup>();
        map.put("group", group);
        String json = JsonUtil.getInstance().obj2json(map);
        System.out.println(json);
        String back = WechatKit.sendPost(url, json,"application/json");
        System.out.println(back);
    }

    @Override
    public List<WechatGroup> queryAll() {
        String url = WechatFianlValue.QUERY_ALL;
        url = url.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
        String back = WechatKit.sendGet(url);
        System.out.println(back);
        Map<String, List<Map>> map = (Map<String, List<Map>>)JsonUtil.getInstance().json2obj(back, Map.class);
        List<Map> mapList = map.get("groups");
        List<WechatGroup> wechatGroupList = new ArrayList<WechatGroup>();
        for(Map m : mapList){
            WechatGroup g = new WechatGroup();
            g.setId((Integer) m.get("id"));
            g.setName((String)m.get("name"));
            g.setCount((Integer)m.get("count"));
            wechatGroupList.add(g);
        }
        return wechatGroupList;
    }

    /**
     * 查询某用户所在的分组
     * 返回的数据仅仅只有groupid，没有groupname和count等信息，所以必须通过queryAll来获取全部信息
     * @param openid 用户的openid
     * @return Wgroup对象
     */
    @Override
    public WechatGroup queryUserGroup(String openid) {
        String url = WechatFianlValue.QUERY_USER_GROUP;
        url = url.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
        Map<String, String> map = new HashMap<String ,String>();
        map.put("openid", openid);
        String json = JsonUtil.getInstance().obj2json(map);
        String back = WechatKit.sendPost(url, json, "application/json");

        Map<String, Integer> backmap = (Map<String, Integer>)JsonUtil.getInstance().json2obj(back, Map.class);
        int groupid = backmap.get("groupid");
        //获取全部分组信息
        List<WechatGroup> wechatGroupList = queryAll();
        for(WechatGroup g : wechatGroupList){
            if (g.getId() == groupid){
                return g;
            }
        }
        return null;
    }

    @Override
    public void updateGroupName(int gid, String name) {
        String url = WechatFianlValue.UPDATE_GROUP_NAME;
        url = url.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
        Map<String, WechatGroup> map = new HashMap<String, WechatGroup>();
        WechatGroup group = new WechatGroup();
        group.setId(gid);
        group.setName(name);
        map.put("group", group);
        String json = JsonUtil.getInstance().obj2json(map);
        String back = WechatKit.sendPost(url, json, "application/json");
        System.out.println(back);
    }

    @Override
    public void moveUser2Group(String openid, int groupid) {
        String url = WechatFianlValue.MOVE_USER_GROUP;
        url = url.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
        Map<String, String> map = new HashMap<String, String>();
        map.put("openid", openid);
        map.put("to_groupid", groupid + "");
        String json = JsonUtil.getInstance().obj2json(map);
        String back = WechatKit.sendPost(url, json, "application/json");
        System.out.println(back);
    }

    @Override
    public void moveUsers2Group(List<String> openidList, int groupid) {
        String url = WechatFianlValue.MOVE_USERS_GROUP;
        url = url.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("openid_list", openidList);
        map.put("to_groupid", groupid+"");
        String json = JsonUtil.getInstance().obj2json(map);
        String back = WechatKit.sendPost(url, json, "application/json");
        System.out.println(back);
    }

    @Override
    public void deleteGroup(int groupid) {
        String url = WechatFianlValue.DELETE_GROUP;
        url = url.replace("ACCESS_TOKEN", WechatContext.getAccessToken());
        Map<String, Object> map = new HashMap<String, Object>();
        WechatGroup group = new WechatGroup();
        group.setId(groupid);
        map.put("group", group);
        String json = JsonUtil.getInstance().obj2json(map);
        String back = WechatKit.sendPost(url, json, "application/json");
        System.out.println(back);
    }
}
